package com.yycx.module.bus.provider.handler.process.task;

import com.yycx.bpm.provider.common.FlowableHelp;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.admin.provider.service.BaseUserService;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.BusInvoiceunit;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.entity.BusSupplier;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.BusDeptService;
import com.yycx.module.bus.provider.service.BusInvoiceunitService;
import com.yycx.module.bus.provider.service.BusPaymentService;
import com.yycx.module.bus.provider.service.BusSupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 厂家返款(退垫付款)审批任务列表
 *
 * @author zyf
 */
@Component
public class RefundTaskTodoListHandler2 implements PageInterceptor<BusPayment> {

    @Autowired
    private BusPaymentService paymentService;
    @Autowired
    private BusSupplierService supplierService;

    @Autowired
    private BusDeptService deptService;

    @Autowired
    private BaseUserService userService;

    @Autowired
    private BusInvoiceunitService invoiceunitService;

    @Autowired
    private FlowableHelp flowableHelp;

    @Override
    public void prepare(CriteriaQuery<BusPayment> cq, PageParams pageParams, EntityMap params) {
        Long companyId = OpenHelper.getCompanyId();
        cq.eq("payment.companyId", companyId);
        cq.in(true, "paymentState", 1);
        cq.like(BusPayment.class, "paymentNo");
        cq.in("amountType", AmountTypeEnum.Refund.getCode(), AmountTypeEnum.SbReturn.getCode());
        cq.orderByDesc("createTime");
    }

    /**
     * 审批列表数据扩展
     *
     * @param cq
     * @param result
     * @param extra
     * @return
     */
    @Override
    public List<EntityMap> complete(CriteriaQuery<BusPayment> cq, List<EntityMap> result, EntityMap extra) {
        if (FlymeUtils.isNotEmpty(result)) {
            for (EntityMap entityMap : result) {
                Long paymentId = entityMap.getLong("paymentId");
                Long userId = entityMap.getLong("userId");
                //单据类型
                String amountType = entityMap.get("amountType");
                BaseUser user = userService.getById(userId);
                Long deptId = entityMap.get("deptId");

                //厂家返款
                if (AmountTypeEnum.Refund.getCode().equals(amountType)) {
                    getRefundMap(entityMap, entityMap);
                }
                //厂家退保证金
                if (AmountTypeEnum.SbReturn.getCode().equals(amountType)) {
                    getSbReturnMap(entityMap, entityMap);
                }

                String processInstanceId = entityMap.get("processInstanceId");
                EntityMap taskMap = flowableHelp.getTaskInfo(processInstanceId);
                if (FlymeUtils.isNotEmpty(taskMap)) {
                    entityMap.putAll(taskMap);
                }
                if (FlymeUtils.isNotEmpty(deptId)) {
                    BusDept dept = deptService.getById(deptId);
                    entityMap.put("deptName", dept.getDeptName());
                }

            }
        }
        return result;
    }

    /**
     * 查询厂家返款信息
     *
     * @param payment
     * @param map
     */
    private void getRefundMap(EntityMap payment, EntityMap map) {
        Long invoiceUnitId = payment.getLong("invoiceUnitId");
        Long supplierId = payment.getLong("supplierId");
        //开票税金ID
        Long parentId = payment.getLong("parentId");
        BusSupplier supplier = supplierService.getById(supplierId);
        BusInvoiceunit invoiceunit = invoiceunitService.getById(invoiceUnitId);
        BusPayment invoice = paymentService.getById(parentId);
        if (FlymeUtils.isNotEmpty(invoiceunit)) {
            map.put("unitName", invoiceunit.getInvoiceunitName());
        }
        if (FlymeUtils.isNotEmpty(supplier)) {
            map.put("supplierName", supplier.getSupplierName());
        }
        if (FlymeUtils.isNotEmpty(invoice)) {
            //开票金额
            map.put("invoiceAmount", invoice.getInvoiceAmount());
            map.put("invoiceDate", invoice.getAmountDate());
        }
    }

    /**
     * 查询厂家返款信息
     *
     * @param payment
     * @param map
     */
    private void getSbReturnMap(EntityMap payment, EntityMap map) {
        Long supplierId = payment.getLong("supplierId");

        BusSupplier supplier = supplierService.getById(supplierId);
        if (FlymeUtils.isNotEmpty(supplier)) {
            map.put("supplierName", supplier.getSupplierName());
        }
    }
}
